package com.ys.servers.gateway.verticle;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.http.ServerWebSocket;
import io.vertx.core.Handler;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.core.buffer.Buffer;

/**
 * Created by taotao on 2017/3/31.
 */
public class WebSocketGateWayVerticle extends AbstractVerticle {


    private static final Logger logger = LoggerFactory.getLogger(RestfulGateWayVerticle.class);


    @Override
    public void start(Future<Void> future) throws Exception {
        super.start();
        System.out.println("WebSocketGateWayVerticle  not finished ,just test.");
        final Pattern chatUrlPattern = Pattern.compile("/chat/(\\w+)");
        // final EventBus eventBus = vertx.eventBus();
        //  final Logger logger = container.logger();


        vertx.createHttpServer().websocketHandler(new Handler<ServerWebSocket>() {
            @Override
            public void handle(final ServerWebSocket ws) {
                final Matcher m = chatUrlPattern.matcher(ws.path());
                if (!m.matches()) {
                    ws.reject();
                    return;
                }

                final String chatRoom = m.group(1);
                final String id = ws.textHandlerID();
                logger.info("registering new connection with id: " + id + " for chat-room: " + chatRoom);
                //vertx.sharedData().getSet("chat.room." + chatRoom).add(id);

                ws.closeHandler(new Handler<Void>() {
                    @Override
                    public void handle(final Void event) {
                        logger.info("un-registering connection with id: " + id + " from chat-room: " + chatRoom);
                        //      vertx.sharedData().getSet("chat.room." + chatRoom).remove(id);
                    }
                });
                //   ws.handler()
                ws.handler(new Handler<Buffer>() {
                    @Override
                    public void handle(final Buffer data) {
                        int i = 0;
                        System.out.println("out");

                   /*     ObjectMapper m = new ObjectMapper();
                        try {
                            JsonNode rootNode = m.readTree(data.toString());
                            ((ObjectNode) rootNode).put("received", new Date().toString());
                            String jsonOutput = m.writeValueAsString(rootNode);
                            logger.info("json generated: " + jsonOutput);
                            for (Object chatter : vertx.sharedData().getSet("chat.room." + chatRoom)) {
                                eventBus.send((String) chatter, jsonOutput);
                            }
                        } catch (IOException e) {
                            ws.reject();
                        }*/
                    }
                });

            }
        }).listen(7788);


        future.complete();
    }

    public void handle(ServerWebSocket serverWebSocket) {

    }

}
